#! /usr/bin/python
#                   _ooOoo_                     
#                  o8888888o                    
#                  88" . "88                    
#                  (| -_- |)                    
#                  O\  =  /O                    
#               ____/`---'\____                 
#             .'  \\|     |//  `.               
#            /  \\|||  :  |||//  \              
#           /  _||||| -:- |||||-  \             
#           |   | \\\  -  /// |   |             
#           | \_|  \---/  |   |             
#           \  .-\__  `-`  ___/-. /             
#         ___`. .'  /--.--\  `. . __            
#      ."" '<  `.___\_<|>_/___.'  >'"".      
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |       
#     \  \ `-.   \_ __\ /__ _/   .-` /  /       
#======`-.____`-.___\_____/___.-`____.-'======  
#                   `=---='                     
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
#         Buddha bless me for no bug            
#                                               
####################################################################################
#   Filename    :  StarLsf.py                                                      
#   Author      :  Shawn Tan                                                        
#   Date        :  2017.2.23                                                        
#   Description :                                                                   
#   History     :  2017.2.23             creat the document            Shawn Tan    
####################################################################################

from StarMisc import *
from StarRE   import *
from StarInfo import *
import commands, os

__author__ = 'ST_CAT'

def openlava_job_status(status):
  
  headers=[]
  comments=[]
  st={}
  l=0
  for i in status.readlines():
    if l==0:
      headers=' '.join(i.split()).split()
      l=1
    else:
      comments=' '.join(i.split()).split()

  for i in range(len(headers)):
    st[headers[i]] = comments[i]

  return st['STAT']

def openlava_submitter(option, job):
  lsf_opt = option.LSF_OPT !=None and option.LSF_OPT or '' 
  lsf_cmd = 'bsub '+lsf_opt+' '+job
  print info011+'  :  '+job
  (status, result) = commands.getstatusoutput(lsf_cmd)

  if bid_re.search(result):
    bid_m=bid_re.search(result)
    bid = bid_m.group()

  return bid

def openlava_monitor(option, job_lib, status, ls, TYPE='COM'):

  exception_status = [
  'EXIT',
  'UNKWN',
  'ZOMBI'
  ]

  onging_status = [
  'PEND',
  'PSUSP',
  'RUN',
  'USUSP',
  'SSUSP'
  ]

  if TYPE == 'COM':
    for i in status:
      bid = job_lib[i]
      lsf_cmd ='bjobs '+bid
      status_info = os.popen(lsf_cmd)
      Status = openlava_job_status(status_info)

      if Status in exception_status:
        status[i] = 'Fail'
        assert False,lsf000+i
      elif Status in onging_status: 
        status[i] = 'Running'
      elif Status == 'DONE':
        status[i] = 'Success'

    if 'Running' not in status.values(): 
      return False
    else:
      return True
  
  elif TYPE == 'SIM':

    pending_case = 0

    for i in job_lib: 
      seed    = job_lib[i].attributes['SIMULATION']['seed'][0]
      session = job_lib[i].sessionname
      group   = job_lib[i].groupname
      test    = job_lib[i].testname
      sim_dir = solve_dir(option, 'FIND_TEST', job_lib[i], seed)
      bid     = i

      lsf_cmd ='bjobs '+bid
      status_info = os.popen(lsf_cmd)
      Status = openlava_job_status(status_info)

      if Status in exception_status:
        status[session][group][test][seed] = 'FAIL'
      elif Status in onging_status: 
        status[session][group][test][seed] = 'UNKNOWN'
        pending_case += 1
      elif Status == 'DONE':
        scan_cmd = script_writer(option, job_lib[i], 0, 'SCAN', seed)  
        result = os.system(scan_cmd)
        result >> 8
        status[session][group][test][seed] = result == 0 and 'PASS' or 'FAIL'
        ls[sim_dir] = 'DONE'

    if pending_case == 0:
      return False
    else:
      return True

